//
//  YRColorfulRoundProgressView.m
//  ColorfulCircleProgressViewMaster
//
//  Created by 张常兴 on 15/11/27.
//  Copyright © 2015年 JasonRyan. All rights reserved.
//





#define YRCoverToRadian(angle)              (M_PI * (angle) / 180.f) // 角度转换为弧度

#define UIColorFromRGB(rgbValue)            [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]


#define YR_TRACK_WIDTH                      2.0f           // 默认轨迹宽度
#define YR_PROGRESS_WIDTH                   2.0f           // 默认进度宽度




#import "YRColorfulRoundProgressView.h"


@interface YRColorfulRoundProgressView ()

@property (nonatomic, strong) CAShapeLayer      *trackLayer;            // 轨迹layer

@property (nonatomic, strong) CAShapeLayer      *progressLayer;         // 进度条layer

@property (nonatomic, strong) UIBezierPath      *circlePath;            // 圆形路径

@property (nonatomic, strong) CALayer           *gradientLayer;         // 左半边渐变颜色

@end

@implementation YRColorfulRoundProgressView


- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupDefaultValue];
        [self drawDefaultUI];
    }
    return self;
}

- (void)setupDefaultValue {
    _progress = 0;
    _trackLineWidth = YR_TRACK_WIDTH;
    _progressLineWidth = YR_TRACK_WIDTH;
    _trackColor = colorWithRGB(215, 215, 215);
    _startAngle = YRCoverToRadian(-87);
    _endAngle = YRCoverToRadian(273);
    _colockwise = YES;
}

- (void)drawDefaultUI {
    
    CGPoint center = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
    // 半径： 取 self.frame的宽高最小值进行计算
    CGFloat radius = MIN(CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds)) / 2  - _progressLineWidth / 2 - _trackLineWidth / 2;
    _circlePath = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:_startAngle endAngle:_endAngle clockwise:_colockwise];
    
    _trackLayer = [CAShapeLayer layer];
    _trackLayer.frame = self.bounds;
    _trackLayer.path  = _circlePath.CGPath;
    _trackLayer.lineWidth = _trackLineWidth;
    _trackLayer.strokeColor = _trackColor.CGColor;
//    _trackLayer.lineCap = kCALineCapRound;
    _trackLayer.fillColor = [UIColor clearColor].CGColor;
    [self.layer addSublayer:_trackLayer];
    
    _progressLayer = [CAShapeLayer layer];
    _progressLayer.path = _circlePath.CGPath;
    _progressLayer.frame = self.bounds;
    _progressLayer.lineWidth = _progressLineWidth;
    _progressLayer.strokeColor = kColor_AppNavColor.CGColor;
    _progressLayer.fillColor = [UIColor clearColor].CGColor;
    _progressLayer.strokeEnd = _progress / 100;
    [self.layer addSublayer:_progressLayer];
    
    
}

- (void)updateCirclePath {
    CGPoint center = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
    // 半径:取 self.frame的宽高最小值进行计算
    CGFloat radius = MIN(CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds)) / 2  - _progressLineWidth / 2 - _trackLineWidth / 2;
    _circlePath = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:_startAngle endAngle:_endAngle clockwise:_colockwise];
    _trackLayer.path = _circlePath.CGPath;
    _progressLayer.path = _circlePath.CGPath;
}

- (void)setProgressLineWidth:(CGFloat)progressLineWidth {
    _progressLayer.lineWidth = progressLineWidth;
}

- (void)setTrackLineWidth:(CGFloat)trackLineWidth {
    _trackLayer.lineWidth = trackLineWidth;
}

- (void)setTrackColor:(UIColor *)trackColor {
    _trackLayer.strokeColor = trackColor.CGColor;
}

- (void)setStartAngle:(CGFloat)startAngle {
    _startAngle = startAngle;
    [self updateCirclePath];
}

- (void)setEndAngle:(CGFloat)endAngle {
    _endAngle = endAngle;
    [self updateCirclePath];
}

- (void)setProgress:(CGFloat)progress {
    _progress = MAX(0, MIN(progress, 100));
    __block CAShapeLayer *progressLayer = self.progressLayer;
    dispatch_async(dispatch_get_main_queue(), ^{
        progressLayer.strokeEnd = _progress / 100;
    });
}







@end
